<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpt_system_attribute.class.php');


class TCMS_VPT_System_Attribute_DBC extends TCMS_VPT_System_Attribute implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select template, sa_name, required, sa_export_name, default_value, export from tcms_vpt_system_attributes where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_template($rez_obj->TEMPLATE);
			$this->set_sa_name(stripslashes($rez_obj->SA_NAME));
			$this->set_required($rez_obj->REQUIRED);
			$this->set_sa_export_name(stripslashes($rez_obj->SA_EXPORT_NAME));
			$this->set_default_value(stripslashes($rez_obj->DEFAULT_VALUE));
			$this->set_export($rez_obj->EXPORT);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpt_system_attributes_seq');
		$template = $this->get_template();
		$required = $this->get_required() ? 1 : 0;
		$sa_name = addslashes($this->get_sa_name());
		$sa_export_name = addslashes($this->get_sa_export_name());
		$default_value = addslashes($this->get_default_value());
		$export = $this->get_export() ? 1 : 0;

		if ($id) {
			$sql = "insert into tcms_vpt_system_attributes (id, ".
									      "template, ".
									      "sa_name, ".
									      "required, ".
									      "default_value, ".
									      "sa_export_name, ".
									      "export ".
									      ") ".
								"values ".
									      "($id,".
									      "$template, ".
									      "'$sa_name', ".
									      "$required, ".
									      "'$default_value', ".
									      "'$sa_export_name', ".
									      "$export ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpt_system_attributes_seq');
		}

		return $ret;
	}



	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$template = $this->get_template();
		$required = $this->get_required() ? 1 : 0;
		$sa_name = addslashes($this->get_sa_name());
		$sa_export_name = addslashes($this->get_sa_export_name());
		$default_value = addslashes($this->get_default_value());
		$export = $this->get_export() ? 1 : 0;

		$sql = "update tcms_vpt_system_attributes set ".
						"template = $template, ".
						"sa_name = '$sa_name', ".
						"required = $required, ".
						"default_value = '$default_value', ".
						"sa_export_name = '$sa_export_name', ".
						"export = $export ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}



	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpt_system_attributes where id = $id";
		$dbc->execute($sql);
	}


	public static function select_vpt_sas_for_template(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);
		$ret = array();

		$sql = "select id, sa_name, required, sa_export_name, default_value, export from tcms_vpt_system_attributes where template = $template_id order by required desc";
		$rez = $dbc->execute($sql);

		while ($rez_obj = $rez->fetch_object()) {
			$ret[$rez_obj->ID]['id'] = $rez_obj->ID;
			$ret[$rez_obj->ID]['sa_name'] = $rez_obj->SA_NAME;
			$ret[$rez_obj->ID]['required'] = $rez_obj->REQUIRED;
			$ret[$rez_obj->ID]['export_name'] = $rez_obj->SA_EXPORT_NAME;
			$ret[$rez_obj->ID]['default_value'] = $rez_obj->DEFAULT_VALUE;
			$ret[$rez_obj->ID]['export'] = $rez_obj->EXPORT;
		}

		return $ret;
	}



	public static function is_unique_template_plus_export_name(DB_Connection $dbc, $sa_id, $template_id, $export_name) {
		tangra_if_not_int_throw_e($sa_id);
		tangra_if_not_int_throw_e($template_id);
		$export_name = addslashes($export_name);

		$sql = "select id from tcms_vpt_system_attributes where id <> $sa_id and template = $template_id and sa_export_name = '$export_name'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public function load_by_template_and_export_name(DB_Connection $dbc, $template_id, $export_name) {
		tangra_if_not_int_throw_e($template_id);
		$export_name = addslashes($export_name);

		$ret = false;

		$sql = "select id from tcms_vpt_system_attributes where template = $template_id and sa_export_name = '$export_name'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);

		}

		return $ret;
	}
}